#ifndef H_RCGD_DUAL_H
#define H_RCGD_DUAL_H

typedef struct CM_M{
	unsigned char 	STATUS;
	unsigned char	EXIST;
	char			AliveCounter;
	unsigned char	status, error, opmode, inform;
	unsigned char	LockMode;
	unsigned char	ErrMode;
	unsigned char	RxFlag;
	unsigned char	osc;
	unsigned char	eb;

	unsigned int	send_count;

	time_			time;

	char			priority;
	unsigned char	reference;

	unsigned char	delay;

	int				standby_counter;

} CM_;

#define NONE						0
#define BLOCK_TO_ACTIVE_STANDBY		1
#define PRE_STANDBY_TO_STANDBY		6
#define ACTIVE_TO_BLOCK				2
#define STANDBY_TO_BLOCK			3
#define ACTIVE_TO_STANDBY			4
#define STANDBY_TO_ACTIVE			5

#define PRE_STANDBY_TO_BLOCK		7


typedef struct MM_M{
	unsigned char	StatusEvent;
	unsigned char	AlarmTimer;
	char			SelfBlock;
	char			ForceBlock;
	char			Command_SelfBlock;
	char			Command_ForceBlock;
	char			sig;

	char			priority;
	unsigned char	LockMode;
	unsigned char	reference;
	unsigned char	osc;

} MM_;



#define	DUAL_DATA_HEADER	0xFF
#define	DUAL_DATA_LENGTH	20

typedef struct{
	unsigned char	header;

	unsigned char	length;

	unsigned char	model_id;

	unsigned char	year;
    unsigned char	month;
	unsigned char	day;
	
	unsigned char	hour;
	unsigned char	minute;
    unsigned char	second;
	
	unsigned char	status;
    unsigned char	error;
	unsigned char	opmode;
    unsigned char	inform;
	unsigned char	delay;

	//unsigned char	STATUS;
	//unsigned char	EXIST;
    //unsigned char	reserved[3];
    
	unsigned char	reserved[5];
	unsigned char	checksum;

} dual_data_;



extern CM_			CounterModule;
extern MM_			MyModule;

extern char LOCKEDtoGPS_SPEC_IN;
extern char LOCKEDtoMASTER_SPEC_IN, init_trace_end;
extern int init_trace_counter;

extern char Status_DualRx,PrevStatus_DualRx;


extern void Init_DUAL(void);
extern void DisplayMessage(void);

extern void ReadYourStatus(BYTE *buffer,int *Counter);

extern int GetParameter1(void);
extern int GetParameter2(void);
extern int GetParameter3(void);

#define DEFAULT_BOARD_DELAY	13

#define COUNTER_SWITCH_BY_PRIORITY	(120*3)

extern int SigOnValue;

extern void LogDualDebug(int SIG);
extern void LogDualExt(void);

enum { dpNOT_EXIST=0, dpPWR_UP, dpFAULT, dpWARMUP, dpTOD, dpSigOn, dpBeforActive, dpComplete };

typedef struct{
	int	my_priority_value ;
	int your_priority_value ;
	unsigned int firstActiveCnt;
	int	prev_my_priority_value ;
	int prev_your_priority_value ;
	
}_PseudoActive;

extern _PseudoActive psda;

extern void SendMyStatus();
extern void SendIamDead(void);

extern dual_data_	dual_data;
extern int utod_status_array[2];

#define TRY_NULL				0x00
#define TRY_TESTMODE			0x01
#define PROVE_TESTMODE_OK		0x02
#define PROVE_TESTMODE_FAIL		0x03
#define TRY_A_HIGH				0x04
#define TRY_A_LOW				0x05
#define TRY_B_HIGH				0x06
#define TRY_B_LOW				0x07
#define PROVE_A_HIGH_OK			0x08
#define PROVE_A_HIGH_FAIL		0x09
#define PROVE_A_LOW_OK			0x0A
#define PROVE_A_LOW_FAIL		0x0B
#define PROVE_B_HIGH_WAITING	0x0C
#define PROVE_B_HIGH_OK			0x0D
#define PROVE_B_HIGH_FAIL		0x0E
#define PROVE_B_LOW_WAITING		0x0F
#define PROVE_B_LOW_OK			0x10
#define PROVE_B_LOW_FAIL		0x11
#define TRY_FINALIZE			0x12

extern int dual_test_start;
extern int dual_test_retry;
extern int dual_test_last_step;
extern int dual_test_last_val;

char ReadMySigStatus(void);

#endif
